import React, { Component } from 'react'
import * as server from '../../apis/allgoods'
import '../../styles/allgood.scss'
import {withRouter} from 'react-router-dom'
 class Allgood extends Component {
  constructor(props) {
    super(props);
    this.state = {
      goodlist: []
    }
  }
  async componentDidMount() {
    let res = await server.good_list(this.props.location.state.id)
    this.setState({
      goodlist: res.list
    }, () => {
      // console.log(this.state.goodlist);
    })
    // console.log(res)
  }
  toDetail(id){
    //动态路由 params
    this.props.history.push('/details/'+id)
  }
  render() {
    return (
      <div className='allgood'>
        <p>全部商品</p>
        <div className='inp'>
          <input type="text" placeholder='搜索店铺内商品' />
          <i className='iconfont icon-fangdajing'></i>
        </div>

        <div className="good">
          {
            this.state.goodlist &&
            this.state.goodlist.length &&
            this.state.goodlist.map((item, index) => {
              return (
                <div className="good_item" key={item.id} onClick={()=>{this.toDetail(item.id)}}>
                  <img src={item.pic} alt="" />
                  <div className="name">{item.name}</div>
                  <div className="old_price">原价￥ {item.salesPrice.value}</div>
                  <div className="price">活动价 ￥ {item.salesPrice.value}</div>
                </div>
              )
            })
          }
        </div>
      </div>
    )
  }
}
export default withRouter(Allgood)